--[[ Monster]]

Monster = {}

function Monster:new(x, y)
	local o = o or {}   -- create object if user does not provide one
	o.x = (x*32) or 0
	o.y = (y*32) or 0
	o.direction_x = 0
	o.direction_y = 0
	o.is_moving = false
	o.last_x=0
	o.last_y=0
	o.possibleDirection = {
		left = true,
		right = true,
		down = true,
		left = true,
	}
	
	setmetatable(o, self)
	self.__index = self
	return o
end


function Monster:getMapX()
	return round(self.x/32)
end

function Monster:getMapY()
	return round(self.y/32)
end

function Monster:setPositionMap(x, y)
	self.x = (x*32)
	self.y = (y*32)
end


function Monster:testRandomMove()
	local a = math.random(4)
	if a == 1 then
		if self.possibleDirection.down then
			self.is_moving=true
			self.is_ud_key_pressed=true
			self.direction_y = 1
		end
	elseif a == 2 then
		if self.possibleDirection.right then
			self.is_moving=true
			self.is_lr_key_pressed=true
			self.direction_x = 1
		end
	elseif a == 3 then
		if self.possibleDirection.up then
			self.is_moving=true
			self.is_ud_key_pressed=true
			self.direction_y = -1
		end
	else
		if self.possibleDirection.left  then
			self.is_moving=true
			self.is_lr_key_pressed=true
			self.direction_x = -1
		end
	end
	if not self.is_moving then
		self:testRandomMove()
	end
end

function Monster:autoMove(dt)

	self.is_ud_key_pressed = false
	self.is_lr_key_pressed = false

	if not self.is_moving then 
		local map_player_pos_x = self:getMapX()
		local map_player_pos_y = self:getMapY()
		self.possibleDirection.right = map_player_pos_x+1 < map:getWidth()+1 and isWalkableTile(map.array[map_player_pos_y][map_player_pos_x+1])
		self.possibleDirection.left = map_player_pos_x-1 > 0 and isWalkableTile(map.array[map_player_pos_y][map_player_pos_x-1])
		self.possibleDirection.up = map_player_pos_y-1 > 0 and isWalkableTile(map.array[map_player_pos_y-1][map_player_pos_x])
		self.possibleDirection.down = map_player_pos_y+1 < map:getHeight()+1 and isWalkableTile(map.array[map_player_pos_y+1][map_player_pos_x])
	end
	
	if not self.is_moving then 
		local tmp = CalcMoves(map.array, self:getMapX(), self:getMapY(), player.getMapX(), player.getMapY())
		debugtext = {self:getMapX().." "..self:getMapY().." "..player.getMapX().." "..player.getMapY()}
		if tmp ~= nil then
			debugtext = {tmp[1].x..tmp[1].y}
		end
	end

	if not self.is_moving then
		if (math.random(2) == 1) then
			if self.possibleDirection.up and player.y < self.y then
				self.is_moving=true
				self.is_ud_key_pressed=true
				self.direction_y = -1
			elseif self.possibleDirection.left and player.x < self.x  then
				self.is_moving=true
				self.is_lr_key_pressed=true
				self.direction_x = -1
			elseif self.possibleDirection.down and player.y > self.y  then
				self.is_moving=true
				self.is_ud_key_pressed=true
				self.direction_y = 1
			elseif self.possibleDirection.right and player.x > self.x  then
				self.is_moving=true
				self.is_lr_key_pressed=true
				self.direction_x = 1
			else
				self:testRandomMove()
			end
		else
			if self.possibleDirection.right and player.x > self.x  then
				self.is_moving=true
				self.is_lr_key_pressed=true
				self.direction_x = 1
			elseif self.possibleDirection.down and player.y > self.y  then
				self.is_moving=true
				self.is_ud_key_pressed=true
				self.direction_y = 1
			elseif self.possibleDirection.up and player.y < self.y then
				self.is_moving=true
				self.is_ud_key_pressed=true
				self.direction_y = -1
			elseif self.possibleDirection.left and player.x < self.x  then
				self.is_moving=true
				self.is_lr_key_pressed=true
				self.direction_x = -1
			else
				self:testRandomMove()
			end
		end
	end
	
	-- Placement en X/Y du monster
	if round(self.y)%tileSize ~= 0 or self.is_ud_key_pressed then
		if (math.abs(self.last_y - self.y) < tileSize) then
			self.y = self.y + self.direction_y * 100 * dt
			self.is_moving=true
		else
			self.y = (round(self.y/tileSize)) * tileSize
			self.last_y = self.y
			self.is_moving = false
		end
	else
		self.y = round(self.y)
		self.last_y = self.y
		self.is_moving = false
		self.direction_y = 0
	end
	if round(self.x)%tileSize ~= 0 or self.is_lr_key_pressed then
		if (math.abs(self.last_x - self.x) < tileSize) then
			self.x = self.x + self.direction_x * 100 * dt
			self.is_moving=true
		else 
			self.x = (round(self.x/tileSize)) * tileSize
			self.last_x = self.x
			self.is_moving = false
		end 
	else
		self.x = round(self.x)
		self.last_x = self.x
		self.direction_x = 0
	end
end